#pragma once
#include <QGraphicsView>
#include <QtGui>
#include "Item.h"
#include "../core/src/AssetManager.h"
#include "StartItem.h"
#include "EndItem.h"
#include "QuestionItem.h"
#include "LinkItem.h"
#include "ConstraintItem.h"
#include "Details.h"
#include "Properties.h"

namespace Xps
{
	class LinkItem;
	
	class TreeView : public QGraphicsView
	{
		Q_OBJECT

	private:
		float zoom_steps;
		XpsAssets::AssetManager* am;

		/// stores all items (exept links)
		QMap<int, Item*> items;
		/// check if a link between two items exist
		bool linkExists(Item* i1, Item* i2);
		/// check if item is a parent to another item
		bool isParent(Item* item);
		/// creates a context menu
		void contextMenuEvent(QContextMenuEvent * event);
		/// breaks a link between to items
		void breakLink(Item* i1, Item* i2);
		/// adds a link to two items
		void addLinkItem(Item* i1, Item* i2, int mode);
		/// adds a link to two items
		void addLinkItem(Item* i1, Item* i2, bool ignore = false);
		/// removes every link from a specific item
		void isolateItem(Item* item);
		/// retrieve position for new item creation
		QPointF getCreationPosition();
		/// setup and connect actions
		void setupActions();
		
	public:
		/// action container
		QList<QAction*> actions;
		
		/// specific actions
		QAction* aCreateQuestionItem;
		QAction* aCreateLink;
		QAction* aBreakLink;
		QAction* aDelete;
		QAction* aCreateConstraint;
		
		/// properties widget
		Properties* props;

		/// stores all link items
		QList<LinkItem*> links;	

		TreeView(XpsAssets::AssetManager* am, QWidget *parent = 0);
		~TreeView(void);
		
		void cleanUp();
		

	public slots:
		/// add question item to scene
		Tree::QuestionItem* addQuestionItem(int qid);
		/// add constraint item to scene
		ConstraintItem* addConstraintItem();
		/// add end item to scene
		Item* addEndItem();
		/// add start item to scene
		Item* addStartItem();
		/// remove selected items from scene
		void remSelectedItems();
		/// adds link to two selected items
		void addLinkItem();
		/// breaks the link between two selected items
		void breakLink();
		/// gets called if selection changed updates childmarkers and properties widget
		void selectionChanged();
		/// zooms into the scene
		void zoomIn();
		/// zooms out of the scene
		void zoomOut();
		
		// assetmanager interaction methods
		void addNode(int id);
		void addLink(int id1, int id2, int mode);
		void remAttribute(int id);

	};
}